package com.u3k.app.mid.utils.statistics;

import android.content.Context;
import android.os.SystemClock;
import com.u3k.app.mid.core.ModuleManager;
import com.u3k.app.mid.utils.common.Util;
import com.u3k.app.mid.utils.net.HttpClient;
import com.u3k.app.mid.utils.net.Request;
import com.u3k.mid.am;
import com.u3k.mid.LogUtils;
import com.u3k.mid.bb;
import com.u3k.mid.bc;
import com.u3k.mid.bd;
import com.u3k.mid.be;
import org.json.JSONArray;
import org.json.JSONObject;

public class StatisticsManager {
    public static final String KEY_LOG_EXC = "KEY_LOG_EXC";
    public static String TAG = StatisticsManager.class.getSimpleName();
    static StatisticsManager mInstance;
    public boolean mPostingLog;

    public static StatisticsManager getInstance() {
        synchronized (StatisticsManager.class) {
            try {
                if (mInstance == null) {
                    mInstance = new StatisticsManager();
                }
                StatisticsManager statisticsManager = mInstance;
                return statisticsManager;
            } finally {
                Object obj = StatisticsManager.class;
            }
        }
    }

    private JSONObject getStatisticsArray(Context context, long j) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("reportTime", SystemClock.currentThreadTimeMillis());
            jSONObject.put("channelId", Util.getChannelId(context));
            jSONObject.put("bundleId", Util.getBundleId(context));
            jSONObject.put("appId", Util.getAppId(context));
            jSONObject.put("shellVer", Util.getShellVersion(context));
            jSONObject.put("middleVer", Util.getMiddleVersion(context));
            jSONObject.put("token", Util.getUserToken(context));
        } catch (Exception e) {
            LogUtils.LogError(TAG, "getStatisticsArray", e);
        }
        JSONArray jSONArray = new JSONArray();
        for (bd bdVar : bd.b(context, j)) {
            JSONObject jSONObject2 = new JSONObject();
            try {
                jSONObject2.put("moduleVer", bdVar.c);
                jSONObject2.put("moduleName", bdVar.b);
                jSONObject2.put("eventName", bdVar.j);
                jSONObject2.put("eventCtx", bdVar.k);
                jSONObject2.put("cTime", bdVar.l);
                jSONArray.put(jSONObject2);
            } catch (Exception e2) {
                LogUtils.LogError(TAG, "getStatisticsArray", e2);
            }
        }
        try {
            jSONObject.put("statistics", jSONArray);
            return jSONObject;
        } catch (Exception e22) {
            LogUtils.LogError(TAG, "getStatisticsArray", e22);
            return jSONObject;
        }
    }

    public void logEvent(Context context, String str, String str2, String str3) {
        synchronized (this) {
            try {
                bd bdVar = new bd();
                bdVar.g = Util.getChannelId(context);
                bdVar.h = Util.getBundleId(context);
                bdVar.f = Util.getAppId(context);
                bdVar.e = Util.getShellVersion(context);
                bdVar.d = Util.getMiddleVersion(context);
                bdVar.i = Util.getUserToken(context);
                bdVar.c = ModuleManager.getInstance(context).getModuleVersion(str);
                bdVar.b = str;
                bdVar.j = str2;
                bdVar.k = str3;
                bdVar.l = System.currentTimeMillis();
                bdVar.a(context);
            } catch (Exception e) {
                LogUtils.LogError(TAG, "logEvent Exception=", e);
            }
        }
    }

    public void logException(Context context, Exception exception) {
        synchronized (this) {
            LogUtils.LogError(TAG, "StatisticsManager Middle Exception=", exception);
            if (context == null) {
                LogUtils.LogError(TAG, "StatisticsManager Middle mContext==null  e = ", exception);
            } else if (((Boolean) bb.b(context, KEY_LOG_EXC, Boolean.valueOf(false))).booleanValue()) {
                getInstance().logEvent(context, "Middle", "exception", bc.a(exception));
            }
        }
    }

    public void postLog(Context context) {
        synchronized (this) {
            long currentTimeMillis = System.currentTimeMillis();
            LogUtils.LogError(TAG, "postLog  time=" + currentTimeMillis);
            if (this.mPostingLog) {
                LogUtils.LogError(TAG, "postLog is posting log, return");
            } else {
                this.mPostingLog = true;
                JSONObject statisticsArray = getStatisticsArray(context, currentTimeMillis);
                LogUtils.LogError(TAG, "postLog  url=" + am.a(context) + "/api/unify/cmn/v1/statics");
                LogUtils.LogError(TAG, "postLog  params=" + statisticsArray.toString());
                new HttpClient().request(new Request().url(am.a(context) + "/api/unify/cmn/v1/statics").defaultHeader().post().addJson(statisticsArray.toString()), new be(this, context, currentTimeMillis));
            }
        }
    }
}
